import uuid
import datetime

from PySide2.QtWidgets import *
from PySide2.QtCore import QFile, QStringListModel, Qt
from PySide2.QtUiTools import QUiLoader

import Logger
import Util
from ExcelData import EData

import ICodeGenerator
import CodeJavaGenerator

class CoderDialog:
    txtDirJavaFile: QLineEdit
    txtDirLayaFile: QLineEdit
    txtDirUnityFile: QLineEdit
    logger: Logger

    def __init__(self):
        super(CoderDialog, self).__init__()
        self.edata = EData()
        self.logger = Logger.Logger()
        # 从文件中加载UI定义
        qfile = QFile("data\\coder.ui")
        qfile.open(QFile.ReadOnly)
        qfile.close()
        # 从UI定义中动态创建一个相应的窗口对象, 将其内部文件定义为self.ui.button
        self.ui = QUiLoader().load(qfile)

        self.txtDirJavaFile = self.ui.txtDirJavaFile
        self.txtDirLayaFile = self.ui.txtDirLayaFile
        self.txtDirUnityFile = self.ui.txtDirUnityFile

        self.ui.btnClose.clicked.connect(self.onBthCloseClick)
        self.ui.btnGenJava.clicked.connect(self.onBthGenJavaClick)
        self.ui.btnGenLaya.clicked.connect(self.onBthGenLayaClick)
        self.ui.btnGenUnity.clicked.connect(self.onBthGenUnityClick)


    def show(self):
        print("打开生成UI")
        self.loadFromData()
        self.ui.show()

    def onBthCloseClick(self):
        self.ui.close()

    def loadFromData(self):
        self.txtDirJavaFile.setText(self.edata.setting.defDirJavaFile)
        self.txtDirLayaFile.setText(self.edata.setting.defDirLayaFile)
        self.txtDirUnityFile.setText(self.edata.setting.defDirUnityFile)

    def onBthGenJavaClick(self):
        # self.syncCheckStateToExcelData()  # 同步勾选状态
        exportTime = datetime.datetime.now()
        exportVersion = uuid.uuid1()
        gen:ICodeGenerator = CodeJavaGenerator.CodeJavaGenerator()
        if gen.loadTemplate:
            result = gen.generateCode(str(exportVersion), exportTime)
            if result:
                self.logger.logAllDone("勾选项目已经全部导出Java代码！请同步到项目目录 (^_−)☆")
                # 打开文件夹
                voPath = self.edata.setting.defDirJavaFile
                if not voPath.endswith("\\"):
                    voPath += "\\"
                Util.openFilePath(voPath)



    def onBthGenLayaClick(self):
        pass

    def onBthGenUnityClick(self):
        pass